package cn.nbt.busManage.controller;


import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.PointDto;
import cn.nbt.busManage.model.dto.RoleDto;
import cn.nbt.busManage.model.dto.UserDto;
import cn.nbt.busManage.model.entity.Role;
import cn.nbt.busManage.model.entity.User;
import cn.nbt.busManage.model.vo.PointVo;
import cn.nbt.busManage.model.vo.UserVo;
import cn.nbt.busManage.service.RoleService;
import cn.nbt.busManage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController()
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     * @return ResponseResult<Object>
     */
    @GetMapping("/list")
    public ResponseResult<Object> findAll(){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<UserDto> all = userService.findAll();
            ArrayList<UserVo> userVos = new ArrayList<>();
            for (UserDto userDto : all) {
                UserVo userVo = new UserVo(userDto);
                userVos.add(userVo);
            }
            result.Success("查询成功",userVos);
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }
    /**
     * 根据信息查询用户
     * @param query 查询信息（名称或id）
     * @return 用户信息
     */
    @GetMapping("/findByQuery")
    public ResponseResult<Object> findByQuery(@RequestParam(name = "query") String query) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<UserDto> userDtoList = userService.findByQuery(query);
            if (userDtoList == null) {
                result.BadRequest();
                return result;
            }
            ArrayList<UserVo> userVos = new ArrayList<>();
            for (UserDto userDto : userDtoList) {
                userVos.add(new UserVo(userDto));
            }
            result.Success("查询成功",userVos);
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }
    /**
     * 根据id查询用户
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            UserDto userDto = userService.findById(id);
            if (userDto == null) {
                result.BadRequest();
                return result;
            }
            UserVo userVo = new UserVo(userDto);
            result.Success("查询成功",userVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 保存用户信息
     * @param userVo 用户信息
     */
    @PostMapping("/save")
    public ResponseResult<Object> save(@RequestBody UserVo userVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            UserDto userDto = new UserDto(userVo);
            userService.save(userDto);
            result.Success("保存成功");
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }
    /**
     * 删除用户信息
     * @param id 用户id
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> delete(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            userService.delete(id);
            result.Success("删除成功");
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 更新用户信息
     * @param userVo 用户信息
     */
    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestBody UserVo userVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            UserDto userDto = new UserDto(userVo);
            UserDto dto = userService.findById(userDto.getId());
            if(!dto.getUserName().equals(userDto.getUserName())){
                UserDto byUserName = userService.findByUserName(userDto.getUserName());
                if(byUserName != null){
                    result.BadRequest("用户名已存在");
                    return result;
                }
            }
            userService.update(userDto);
            result.Success("更新成功");
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    @GetMapping("/login")
    public ResponseResult<Object> login(
            @RequestParam(name = "username") String userName,
            @RequestParam(name = "password") String password)
    {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            UserDto userDto = userService.findByUserNameAndPassword(userName, password);
            if (userDto == null) {
                result.Error("该用户不存在或密码错误！");
                return result;
            }
            List<RoleDto> roleDtos = userService.findRolesById(userDto.getId());
            result.Success("登录成功",roleDtos);
        }catch (Exception e){
            result.Error("出错啦！",e.getMessage());
        }
        return result;
    }

}
